﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Rodunu.Business.Models.User;

namespace Rodunu.Clients.WPF.Settings
{
    public class LogOnEventArgs : EventArgs
    {
        public User LoggedOnUser { get; set; }

        public bool RememberMe { get; set; }

        public bool RememberPassword { get; set; }

        public bool AutoLogIn { get; set; }
    
        public LogOnEventArgs(User loggedOnUser, bool rememberMe, bool rememberPassword, bool autoLogIn)
        {
            LoggedOnUser = loggedOnUser;
            RememberMe = rememberMe;
            RememberPassword = rememberPassword;
            AutoLogIn = autoLogIn;
        }
    }

    /// <summary>
    /// Interaction logic for LogonCreate.xaml
    /// </summary>
    public partial class LogonCreate : UserControl
    {
        private User _loggedOnUser;

        public event EventHandler<LogOnEventArgs> UserLoggedOn;
        public event EventHandler<LogOnEventArgs> UserCreated;

        public LogonCreate()
        {
            InitializeComponent();

            CreateButton.Click += CreateButton_Click;
            LoginButton.Click += LoginButton_Click;
        }

        void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            _loggedOnUser = new User();
            _loggedOnUser.UserName = LoginUserName.Text;
            _loggedOnUser.Password = LoginPassword.Password;

            if (UserLoggedOn != null)
                UserLoggedOn(sender, new LogOnEventArgs(_loggedOnUser, 
                    Convert.ToBoolean(LoginRememberMe.IsChecked), 
                    Convert.ToBoolean(LoginRememberPassword.IsChecked), 
                    Convert.ToBoolean(LoginAutoLogin.IsChecked)));
        }

        void CreateButton_Click(object sender, RoutedEventArgs e)
        {
            _loggedOnUser = new User();
            if (CreateUserName.Text.Length > 0)
            {
                if (CreatePassword.Password.Length > 0)
                {
                    if (CreatePasswordReEnter.Password.Length > 0)
                    {
                        if (CreatePasswordReEnter.Password == CreatePassword.Password)
                        {
                            _loggedOnUser.UserName = CreateUserName.Text;
                            _loggedOnUser.Password = CreatePassword.Password;
                            _loggedOnUser.FirstName = CreateFirstName.Text;
                            _loggedOnUser.LastName = CreateLastName.Text;

                            if (UserCreated != null)
                                UserCreated(sender, new LogOnEventArgs(_loggedOnUser, 
                                    Convert.ToBoolean(CreateRememberMe.IsChecked),
                                    Convert.ToBoolean(CreateRememberPassword.IsChecked), 
                                    Convert.ToBoolean(CreateAutoLogin.IsChecked)));
                        }
                        else
                        {
                            MessageBox.Show("The 2 passwords entered do not match");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Please enter the second password");
                    }
                }
                else
                {
                    MessageBox.Show("Please enter a password");
                }
            }
            else
            {
                MessageBox.Show("Please enter a user name");
            }
        }
    }
}
